import 'package:flutter/material.dart';
import 'package:flutter_pro/base/base_state.dart';
import 'package:flutter_pro/common/macro.dart';
import 'package:flutter_pro/common/tools/segmented_view.dart';

class PageMoreWidget extends StatefulWidget {
  const PageMoreWidget({super.key});

  @override
  State<PageMoreWidget> createState() => _PageMoreWidgetState();
}

class _PageMoreWidgetState extends BaseState<PageMoreWidget> {
  late int curIndex = 0;
  late PageController pageCon = PageController(initialPage: 0);
  late PageController segCon = PageController(initialPage: 0);
  final List<String> titles = [
    "最新0",
    "最新1",
    "最新2",
    "最新3",
    "最新4",
    "最新5",
    "最新6",
    "最新7",
    "最新8",
    "最新9",
    // "最新10",
    // "最新11",
    // "最新12"
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    title = "多页面";
    barColor = colorfff;
    backgroundColor = colorf5f5f5;
  }

  _reloadPage(index) {
    if (index == curIndex) {
      return;
    }

    curIndex = index;
    pageCon.animateToPage(curIndex,
        duration: const Duration(milliseconds: 300), curve: Curves.linear);
    print("当前_$curIndex");
    setState(() {});
  }

  @override
  Widget initBody(BuildContext context) {
    // TODO: implement initBody
    return Column(
      children: [
        SegmentedView(
          titles: titles,
          itemHeight: 50,
          itemSpacing: 15,
          controller: segCon,
          titleNormal: const TextStyle(
            color: color999,
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
          titleSelected: const TextStyle(
            color: color333,
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ),
          defaultSelectedIndex: curIndex,
          onPressed: (index) {
            if (index == curIndex) {
              return;
            }

            curIndex = index;
            pageCon.jumpToPage(curIndex);
            print("当前_$curIndex");
            setState(() {});
          },
        ),
        Expanded(
          child: PageView.builder(
            itemCount: titles.length,
            controller: pageCon,
            onPageChanged: (index) {
              if (index == curIndex) {
                return;
              }

              curIndex = index;
              segCon.animateTo(curIndex * 50,
                  duration: const Duration(milliseconds: 300),
                  curve: Curves.linear);
              print(
                  "滚动_${curIndex}_______________${pageCon.offset}______________${pageCon.position.maxScrollExtent}");
              setState(() {});
            },
            itemBuilder: (BuildContext context, int index) {
              return Container(
                padding: const EdgeInsets.all(20),
                color: Colors.grey,
                child: Text("页面$index"),
              );
            },
          ),
        ),
      ],
    );
  }
}
